/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.utilitarioJavaMail;

import java.io.BufferedReader;  
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.InternetAddress;  
  
public class InboxReader {  
  
    /** 
     * "printMessage()" method to print a message. 
     */  
    public static void printMessage(Message message) {  
        try {  
            // Get the header information  
            String from = ((InternetAddress) message.getFrom()[0])  
                    .getPersonal();  
            if (from == null) {
                from = ((InternetAddress) message.getFrom()[0]).getAddress();
            }  
  
            System.out.println("FROM: " + from);  
            String subject = message.getSubject();  
            System.out.println("SUBJECT: " + subject);  
  
            // -- Get the message part (i.e. the message itself) --  
            Part messagePart = message;  
            Object content = messagePart.getContent();  
  
            // -- or its first body part if it is a multipart message --  
            if (content instanceof Multipart) {  
                messagePart = ((Multipart) content).getBodyPart(0);  
                System.out.println("CONTEÚDO: "  
                        + messagePart.getContent().toString());  
            }  
  
            // -- Get the content type --  
            String contentType = messagePart.getContentType();  
  
            // -- If the content is plain text, we can print it --  
            System.out.println("CONTENT:" + contentType);  
            if (contentType.startsWith("text/plain")  
                    || contentType.startsWith("text/html")) {  
                InputStream is = messagePart.getInputStream();  
  
                BufferedReader reader = new BufferedReader(  
                        new InputStreamReader(is));  
  
                String thisLine = reader.readLine();  
                while (thisLine != null) {  
                    thisLine = reader.readLine();  
                }  
            }  
  
        else {  
                byte[] buf = new byte[4096];  
  
                // Define o caminho que salvará o arquivo.  
                //String caminhoBase = System.getProperty("user.dir") +"\\src\\xml\\";
                String caminhoBase = "C:\\WebApplication\\src\\java\\config\\tmp\\";
                System.out.println(caminhoBase);
                Multipart multi = (Multipart) content;  
  
                  
                  
                for (int i = 0; i < multi.getCount(); i++) {  
                      
                    String nomeDoArquivo = multi.getBodyPart(i).getFileName();  
                      
                    if (nomeDoArquivo != null) {  
                          
                        // Imprime o caminho já com o nome do arquivo para ter certeza, e somente se tiver arquivos.  
                        System.out.println("caminho arr " + caminhoBase+nomeDoArquivo);  
                          
                        InputStream is = multi.getBodyPart(i).getInputStream();  
                          
                        FileOutputStream fos = new FileOutputStream(caminhoBase + nomeDoArquivo);  
                          
                        int bytesRead;  
                        while ((bytesRead = is.read(buf)) != -1) {  
                            fos.write(buf, 0, bytesRead);  
                        }  
                          
                        fos.close();  
                    }  
                }  
            }  
  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    }  
  
} 